package com.dgshanger.sanhxiaofeisc.mvp.presenter

import android.text.TextUtils
import com.dgshanger.sanhxiaofeisc.App.Companion.application
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.mvp.contract.EditPayPasswordContract
import com.dgshanger.sanhxiaofeisc.mvp.model.EditPayPasswordModel
import com.xx.baseuilibrary.mvp.BaseRxMvpPresenter
import com.xx.baseutilslibrary.network.rx.RxHttpObserver
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observers.DefaultObserver
import java.util.concurrent.TimeUnit

/**
 * EditPayPasswordPresenter
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/4/28 11:40.
 */

class EditPayPasswordPresenter : BaseRxMvpPresenter<EditPayPasswordContract.Model, EditPayPasswordContract.View>(), EditPayPasswordContract.Presenter {

    override fun sendCode() {
        val phone = getView()!!.phone
        if (TextUtils.isEmpty(phone)) {
            getView()!!.showToast(application.getString(R.string.please_input_phone_number))
            return
        }
        if (phone.length != 11) {
            getView()!!.showToast(application.getString(R.string.please_input_right_phone))
            return
        }
        getModel().sendCode(phone, object : RxHttpObserver<Any>() {
            override fun onCompleted(msg: String?, entity: Any?) {
                getView()?.showToast(msg)
                //开启验证码倒计时
                startTimer()
            }

            override fun onError(error: String?) {
                getView()?.showToast(error)
            }

            override fun onLoadingStart() {
                attachObserver(this)
                getView()?.showLoadingDialog()
            }

            override fun onLoadingFinish() {
                getView()?.dismissLoadingDialog()
            }

        })

    }

    private fun startTimer() {
        val timeCount = 60
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take((timeCount + 1).toLong())
                .map { aLong -> timeCount - aLong!! }
                .doOnSubscribe {
                    //发送数据中,将发送按钮状态设置为不可用
                    getView()?.setSendBtnEnable(false)
                }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DefaultObserver<Long>() {
                    override fun onNext(value: Long?) {
                        getView()?.setSendBtnText("(" + value + "s)")
                    }

                    override fun onError(e: Throwable) {
                        getView()?.setSendBtnEnable(true)
                        getView()?.setSendBtnText(application.getString(R.string.retry))
                    }

                    override fun onComplete() {
                        getView()?.setSendBtnEnable(true)
                        getView()?.setSendBtnText(application.getString(R.string.resend))
                    }
                })
    }

    override fun modifyPaypwd() {
        val phone = getView()!!.phone
        val verifyCode = getView()!!.verify_code
        val newPwd = getView()!!.new_pwd
        val confirmPwd = getView()!!.confirm_pwd
        if (TextUtils.isEmpty(phone) || phone.length != 11) {
            getView()?.showToast(application.getString(R.string.please_input_phone_number))
            return
        }

        if (TextUtils.isEmpty(verifyCode)) {
            getView()!!.showToast(application.getString(R.string.please_input_vcode))
            return
        }

        if (TextUtils.isEmpty(newPwd)) {
            getView()!!.showToast(application.getString(R.string.please_input_new_pw))
            return
        }
        if (TextUtils.isEmpty(confirmPwd)) {
            getView()!!.showToast(application.getString(R.string.please_sure_new_pw))
            return
        }

        getModel().modifyPaypwd(newPwd, confirmPwd, verifyCode, object : RxHttpObserver<Any>() {
            override fun onCompleted(msg: String?, entity: Any?) {
                getView()?.showToast(msg)
                getView()?.finishActivity()
            }

            override fun onError(error: String?) {
                getView()?.showToast(error)
            }

            override fun onLoadingStart() {
                attachObserver(this)
                getView()?.showLoadingDialog()
            }

            override fun onLoadingFinish() {
                getView()?.dismissLoadingDialog()
            }

        })

    }

    override fun createModel(): EditPayPasswordContract.Model = EditPayPasswordModel()
}
